package com.songtech.uitest.module.cancel;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.*;
import com.songtech.uitest.module.propose.ProposeSubmit;
import com.songtech.uitest.module.screen.ScreenSubmit;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MobileCancelConfirm {

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private KakongHandler kakongHandler;

    @Autowired
    private LogInAndOutHandler logInAndOutHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(MobileCancelConfirm.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     * 移动进行需求取消确认。参数指定了是哪方的责任和谁来操作。
     * @param step 类似"00"，十位代表是否同意和哪方的责任：0同意且移动责任，1不同意移动责任，2同意铁塔责任，3不同意铁塔责任
     *个位代表谁登录：0为主账号登录，其它为辅账号登录
     * @throws Exception
     */
    public void mobileCancelOneConfirm(String step) throws Exception{

        Map<String, Boolean> result = stepParser.parseRspAndApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //移动进行需求取消确认
        //菜单打开需求取消确认页面
        menuHandler.clickMenuItem(driver, "铁塔建设", "需求取消确认", "");
        //查询此单
        driver.switchTo().frame("iframeundefined");
        Select taskStatus = new Select(driver.findElement(By.id("taskStatus")));
        taskStatus.selectByVisibleText("待办");
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        Thread.sleep(6000);
        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='提交']")).click();

        //打开并转到需求取消确认提交详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到需求取消确认提交详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);

        driver.switchTo().frame(frame);
        //是否同意
        Select syncResult = new Select(driver.findElement(By.id("syncResult")));
        syncResult.selectByVisibleText(result.get("approval") ? "同意" : "不同意");
        //若不同意，必须要填不同意原因
        Thread.sleep(500);
        if(result.get("approval") == false) {
            driver.findElement(By.id("failMessage")).sendKeys("不同意，不解释");
        }
        //哪方责任
        Select responsible = new Select(driver.findElement(By.id("responsible")));
        responsible.selectByVisibleText(result.get("rspToMobile") ? "移动" : "铁塔");

        driver.findElement(By.id("responsibleType")).sendKeys(result.get("rspToMobile") ? "此取消确认是移动方的责任" : "此取消确认是铁塔方的责任");

        driver.findElement(By.id("auditNote")).sendKeys("提交此需求取消确认去审核");

        driver.findElement(By.id("saveSet")).click();


        Thread.sleep(5000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        Thread.sleep(2000);

        driver.switchTo().defaultContent();

        String rspInfo = result.get("rspToMobile") ? "责任方为移动" : "责任方为铁塔";
        String opt = result.get("approval") ? "同意" : "不同意";
        logger.info(space.getSpace() + "铁塔发起，移动进行取消确认提交成功，" + rspInfo + "，意见为" + opt);

    }


    public void mobileCancel3OneConfirm(String step) throws Exception{

        Map<String, Boolean> result = stepParser.parseRspAndApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //移动进行三方需求取消确认
        //菜单打开移动三方需求取消确认页面
        menuHandler.clickMenuItem(driver, "三方建设", "需求取消确认", "");
        //查询此单
        driver.switchTo().frame("iframeundefined");
        Select taskStatus = new Select(driver.findElement(By.id("taskStatus")));
        taskStatus.selectByVisibleText("待办");
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        Thread.sleep(6000);
        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='提交']")).click();

        //打开并转到移动三方需求取消确认详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到移动三方需求取消确认详细页
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);

        driver.switchTo().frame(frame);
        //是否同意
        Select syncResult = new Select(driver.findElement(By.id("syncResult")));
        syncResult.selectByVisibleText(result.get("approval") ? "同意" : "不同意");
        //若不同意，必须要填不同意原因
        Thread.sleep(500);
        if(result.get("approval") == false) {
            driver.findElement(By.id("failMessage")).sendKeys("不同意，不解释");
        }
        //哪方责任
        Select responsible = new Select(driver.findElement(By.id("responsible")));
        responsible.selectByVisibleText(result.get("rspToMobile") ? "移动" : "三方");

        driver.findElement(By.id("responsibleType")).sendKeys(result.get("rspToMobile") ? "此取消确认是移动方的责任" : "此取消确认是第三方的责任");

        driver.findElement(By.id("auditNote")).sendKeys("提交此需求取消确认去审核");

        driver.findElement(By.id("saveSet")).click();


        Thread.sleep(5000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        Thread.sleep(2000);

        driver.switchTo().defaultContent();

        String rspInfo = result.get("rspToMobile") ? "责任方为移动" : "责任方为第三方";
        String opt = result.get("approval") ? "同意" : "不同意";
        logger.info(space.getSpace() + "第三方发起，移动进行取消确认提交成功，" + rspInfo + "，意见为" + opt);

    }


}
